package lesson_two;

import lesson_two.Dog;

/**
 * @author LvHengChang
 * version 1.0
 * @description:
 */

public class DogProblem {

    public static Dog[] largerThanFourNeighbors(Dog[] dogs) {
        Dog[] returnDogs = new Dog[dogs.length];
        int cnt = 0;

        for (int i = 0; i < dogs.length; i++) {
            if (isBiggestOfFour(dogs, i)) {
                returnDogs[cnt] = dogs[i];
                cnt = cnt + 1;
            }
        }
        return returnDogs;
    }

    public static boolean isBiggestOfFour(Dog[] dogs, int i) {
        boolean isBiggest = true;
        for (int j = -2; j < 2; j += 1) {
            int compareIndex = i + j;

            if (j == 0) {
                continue;
            }

            if (validIndex(dogs, compareIndex)) {
                if (dogs[compareIndex].weightInPounds >= dogs[i].weightInPounds) {
                    isBiggest = false;
                }
            }
        }
        return isBiggest;
    }

    public static boolean validIndex(Dog[] dogs, int i) {
        if (i < 0) {
            return false;
        }
        return i < dogs.length;
    }

    public static void main(String[] args) {
        Dog[] dogs = new Dog[]{
                new Dog(30),
                new Dog(29),
                new Dog(20),
                new Dog(40),
                new Dog(10),
                new Dog(5),
                new Dog(10),
                new Dog(15),
                new Dog(22),
                new Dog(20),
        };
        Dog[] bigDogs1 = largerThanFourNeighbors(dogs);

        for (Dog dog : bigDogs1) {
            System.out.println(dog.weightInPounds + " ");
        }
        System.out.println();
    }
}
